package ntd.util.frame.msg.service.handle.jpush.push;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.BaseResult;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import lombok.Data;
import ntd.util.frame.msg.bo.MsgConfig;

public class JPushHelper {

    private JPushClient jpushClient;
    private MsgConfig msgConfig;

    public JPushHelper(MsgConfig msgConfig) {
        this.msgConfig = msgConfig;
        this.jpushClient = new JPushClient(msgConfig.getSecret(), msgConfig.getAppKey());
    }

    public BaseResult sendMsg(JPushMsgInBO jPushMsgInBO) {
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(jPushMsgInBO.platform) // 这个可以指定对android、ios发送
                .setAudience(jPushMsgInBO.audience)
                .setNotification(jPushMsgInBO.notification)
                .build();
        try {
            BaseResult baseResult = jpushClient.sendPush(payload);
            return baseResult;
        } catch (Exception e) {
        } finally {
            jpushClient.close();
        }
        return null;
    }

    @Data
    public static class JPushMsgInBO {

        private Platform platform = Platform.all();
        private Audience audience = Audience.all();
        private Notification notification;

    }

}